package xy.donate.android.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.room.Room;

import com.gyf.immersionbar.ImmersionBar;

import xy.donate.android.R;
import xy.donate.android.aop.SingleClick;
import xy.donate.android.common.MyActivity;
import xy.donate.android.db.LoginDB;
import xy.donate.android.entity.UserLoginEntity;
import xy.donate.android.helper.InputTextHelper;
import xy.donate.android.http.model.HttpData;
import xy.donate.android.http.request.GetCodeApi;
import xy.donate.android.http.request.RegisterApi;
import xy.donate.android.http.response.LoginBean;
import xy.donate.android.http.response.RegisterBean;
import xy.donate.android.other.IntentKey;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.CountdownView;

import java.util.UUID;

/**
 * author : Xy
 * desc   : 注册界面
 */
public final class RegisterActivity extends MyActivity {

    private EditText mPhoneView;
    private CountdownView mCountdownView;

    private EditText mCodeView;

    private EditText mPasswordView1;
    private EditText mPasswordView2;

    private Button mCommitView;
    private LoginDB loginDB;

    @Override
    protected int getLayoutId() {
        return R.layout.register_activity;
    }

    @Override
    protected void initView() {
        mPhoneView = findViewById(R.id.et_register_phone);
        mCountdownView = findViewById(R.id.cv_register_countdown);
        mCodeView = findViewById(R.id.et_register_code);
        mPasswordView1 = findViewById(R.id.et_register_password1);
        mPasswordView2 = findViewById(R.id.et_register_password2);
        mCommitView = findViewById(R.id.btn_register_commit);
        setOnClickListener(mCountdownView, mCommitView);

        // 给这个 View 设置沉浸式，避免状态栏遮挡
        ImmersionBar.setTitleBar(this, findViewById(R.id.tv_register_title));

        loginDB = Room.databaseBuilder(this, LoginDB.class, "LoginDB")
                .allowMainThreadQueries()
                .build();

        InputTextHelper.with(this)
                .addView(mPhoneView)
                .addView(mCodeView)
                .addView(mPasswordView1)
                .addView(mPasswordView2)
                .setMain(mCommitView)
                .build();
    }

    @Override
    protected void initData() {

    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (v == mCountdownView) {
            if (mPhoneView.getText().toString().length() != 11) {
                toast(R.string.common_phone_input_error);
                return;
            }
            toast(R.string.common_code_send_hint);
            mCountdownView.start();
        } else if (v == mCommitView) {
            if (mPhoneView.getText().toString().length() != 11) {
                toast(R.string.common_phone_input_error);
                return;
            }

            if (!mPasswordView1.getText().toString().equals(mPasswordView2.getText().toString())) {
                toast(R.string.common_password_input_unlike);
                return;
            }
            UserLoginEntity entity = loginDB.loginDao().queryPhone(mPhoneView.getText().toString());
            if (entity != null) {
                toast("该手机号已经注册");
            } else {
                UserLoginEntity loginEntity = new UserLoginEntity();
                loginEntity.setPhone(mPhoneView.getText().toString());
                loginEntity.setPassword(mPasswordView1.getText().toString());
                loginEntity.setUserID(UUID.randomUUID().toString().replace("-",""));
                loginDB.loginDao().insert(loginEntity);
                toast(R.string.register_succeed);
                setResult(RESULT_OK, new Intent()
                        .putExtra(IntentKey.PHONE, mPhoneView.getText().toString())
                        .putExtra(IntentKey.PASSWORD, mPasswordView1.getText().toString()));
                finish();
            }
        }
    }

    @NonNull
    @Override
    protected ImmersionBar createStatusBarConfig() {
        return super.createStatusBarConfig()
                // 不要把整个布局顶上去
                .keyboardEnable(true);
    }

    @Override
    public boolean isSwipeEnable() {
        return false;
    }
}